package com.by.config;


import com.by.job.ClearImgJob;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

@Configuration
public class QuartzConfig {

    //job 做什么事
    @Bean   //把返回值放到袋子里
    public MethodInvokingJobDetailFactoryBean methodInvokingJobDetailFactoryBean(ClearImgJob clearImgJob){
        MethodInvokingJobDetailFactoryBean jobDetailFactoryBean = new MethodInvokingJobDetailFactoryBean();
        //把要做的事情抽出来，放到JobDemo类的printTime方法中
        jobDetailFactoryBean.setTargetObject(clearImgJob);
        jobDetailFactoryBean.setTargetMethod("clearImg");
        return jobDetailFactoryBean;
    }


    //trigger：什么时候做
    @Bean
    public CronTriggerFactoryBean cronTriggerFactoryBean(MethodInvokingJobDetailFactoryBean jobDetailFactoryBean){
        CronTriggerFactoryBean triggerFactoryBean = new CronTriggerFactoryBean();
        //每秒钟执行一次
        triggerFactoryBean.setCronExpression("/30 * * * * ?");
        triggerFactoryBean.setJobDetail(jobDetailFactoryBean.getObject());
        return triggerFactoryBean;
    }


    //scheduler：什么时候做什么事情
    @Bean
    public SchedulerFactoryBean schedulerFactoryBean(CronTriggerFactoryBean triggerFactoryBean){
        SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
        schedulerFactoryBean.setTriggers(triggerFactoryBean.getObject());
        return schedulerFactoryBean;
    }


}
